package com.lagou.service;

import com.alibaba.fastjson.JSON;
import com.lagou.dao.UserDao;
import com.lagou.handler.UserServerHandler;
import com.lagou.util.*;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.I0Itec.zkclient.ZkClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class UserServiceImpl implements UserService {
    public String sayHello(String word) {
        System.out.println("调用成功--参数 "+word);
        UserDao userDao =new UserDao();
        userDao.updateUser();
        return "success";
    }

    public static void startServer(String hostName,int port) throws InterruptedException {
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(bossGroup,workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {

                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
//                        pipeline.addLast(new StringEncoder());
//                        pipeline.addLast(new StringDecoder());
                        pipeline.addLast( new RpcEncoder(RpcResponse.class, new JSONSerializerZDY()));
                        pipeline.addLast( new RpcDecoder(RpcRequest.class, new JSONSerializerZDY()));
                        pipeline.addLast(new UserServerHandler());
                    }
                });

        serverBootstrap.bind(hostName,port).sync();
    }

    public static void createHostToZookeeper(){
        ZkClient zkClient = new ZkClient("114.215.81.54:2181");
        System.out.println("会话被创建了。。");

        boolean exists = zkClient.exists("/serverlist");
        if(!exists){
            zkClient.createPersistent("/serverlist");
        }

        zkClient.createEphemeral("/serverlist/127.0.0.1:8991");
        System.out.println("创建server2节点成功！");
    }

    public static void saveDBInfoToZookeeper(){
        HashMap<String, String> env = new HashMap<>();
        env.put("driver","com.mysql.jdbc.Driver");
        env.put("url","jdbc:mysql://localhost:3306/test");
        env.put("username","root");
        env.put("password","root");
        String json= JSON.toJSONString(env);

        //创建zookeeper节点保存数据库连接信息
        ZkClient zkClient = new ZkClient("114.215.81.54:2181");
        boolean exists = zkClient.exists("/DBInfo");
        if(!exists){
            zkClient.createPersistent("/DBInfo",json);
        }else{
            zkClient.writeData("/DBInfo",json);
        }
    }
}
